//----------------------------------------------------------------------------------------------------------------------
/// \file
/// <summary>Consumer class for Etw events</summary>
// Copyright (c) Microsoft Corporation.  All Rights Reserved.
//----------------------------------------------------------------------------------------------------------------------
#pragma once
#include "EtwProcessor.h"

#include <Windows.h>
#include <Wmistr.h> // In older Windows headers, you have to include Wmistr.h before Evntrace.h.
#include <Evntrace.h>
#include <tdh.h>

namespace Etw { namespace Processor
{
    /// <summary>
    /// Base class for consumers of Etw events
    /// </summary>
    class ETWPROCESSOR_API EtwConsumer abstract
    {
    public:

        virtual ~EtwConsumer()
        {}

        /// <summary>Method to open the trace for processing</summary>
        /// <returns>S_OK on success</returns>
        virtual HRESULT OpenTrace() = 0;

        /// <summary>Method to Process the trace</summary>
        /// <returns>S_OK on success</returns>
        virtual HRESULT ProcessTrace();

        /// <summary>Method to close the trace</summary>
        /// <returns>S_OK on closing the trace</returns>
        HRESULT CloseTrace();

        /// <summary>Method to set the event callback function to be executed</summary>
        /// <param name="pEventCallback">Pointer to the event callback function</param>
        void SetEventCallback(PEVENT_CALLBACK pEventCallback)
        {
            m_pEventCallback = pEventCallback;
        }

        /// <summary>Method to set the event record callback function to be executed</summary>
        /// <param name="pEventCallback">Pointer to the event record callback function</param>
        /// <param name="context">Any context information that can be accessed from the callback</param>
        void SetEventRecordCallback(PEVENT_RECORD_CALLBACK pEventRecordCallback, LPVOID context)
        {
            m_pEventRecordCallback = pEventRecordCallback;
            m_pContext = context;
        }

        /// <summary>Method to retrieve the tick frequency after a call to OpenTrace</summary>
        long long GetTickFrequency();

    protected:
        EtwConsumer() : m_traceHandle(0), m_pContext(NULL), m_pEventCallback(NULL), m_pEventRecordCallback(NULL), m_frequency(0)
        {}

    private:
        EtwConsumer(const EtwConsumer&);//not implemented
        EtwConsumer& operator=(const EtwConsumer&);//not implemented

    protected:
        //Handle to the trace that is opened.
        TRACEHANDLE m_traceHandle;

        //Data variable that can be passed to EVENT_RECORD_VARIABLE
        LPVOID m_pContext;

        //Pointer to EVENT_CALLBACK
        PEVENT_CALLBACK m_pEventCallback;

        //Pointer to EVENT_RECORD_CALLBACK
        PEVENT_RECORD_CALLBACK m_pEventRecordCallback;

        long long m_frequency;
    };
}/* namespace Processor */ } /* namespace Etw */